<?php

namespace app\api\service;

use think\facade\Db;

class CCNames
{
    public function getCCNamesByIp($ip = '182.239.127.137')
    {

        $result = Db::name('ip2region')
            ->where('ip_segment_start', '<=', Db::raw('inet_aton(?)', [$ip]))
            ->where('ip_segment_end', '>=', Db::raw('inet_aton(?)', [$ip]))
            ->order('ip_segment_start', 'desc')
            ->limit(1)
            ->value('rc_id');

        if (!$result) {
            $result = Db::name('ip2region')
                ->where('ip_segment_start', '<=', Db::raw('inet_aton(?)', ['182.239.127.137']))
                ->where('ip_segment_end', '>=', Db::raw('inet_aton(?)', ['182.239.127.137']))
                ->order('ip_segment_start', 'desc')
                ->limit(1)
                ->value('rc_id');
        }

        $ccNameId = Db::name('region_country')
            ->where('id', $result)
            ->value('ccname_id');
        return $ccNameId;
    }
}
